# 1、登录
# 2、添加用户分组——》删除刚才新增的用户分组
# 3、添加是否成功，断言；删除是否成功，断言
# 组件化，元素与脚本分离

# 1、添加用户标签——》删除刚才新增的用户标签
# 2、添加是否成功，断言；删除是否成功，断言


import uuid
import time
from selenium import webdriver
from zhengkexin.基础作业.elements import *
from selenium.webdriver.support.ui import WebDriverWait   #显性时间
from selenium.webdriver.support import expected_conditions as EC   #显性时间


import random
class CRMEB:
    a=uuid.uuid4()
    b=str(a).split("-")[0]
    def __init__(self,driver,url):
        self.driver=driver
        self.driver.get(url)
        self.driver.implicitly_wait(10)
        self.driver.maximize_window()
        
    def login(self,user:str,password:str):
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(login1))[0].send_keys(user)
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(login2))[1].send_keys(password)
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located(login3)).click()  #登录
        
        
    def  add_group(self,name):
        self.name=name
        time.sleep(2)
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(add_group1))[1].click()  # 用户

        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(add_group2))[1].click()   #用户分组
        time.sleep(2)
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located(add_group3))[0].click()    #添加
        time.sleep(3)
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located(add_group4))[2].send_keys(self.name+str(self.b))     #输入
        time.sleep(1)
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located(add_group5))[2].click()  # 确定

    def check_add(self):
        time.sleep(1)
        c=self.driver.find_element(By.XPATH,f"//*[text()='{self.name}{self.b}']").text
        assert c==self.name+str(self.b)
        time.sleep(1)

    def delete_group(self):
        time.sleep(1)
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(delete_group1))[-1].click()
        time.sleep(1)
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(delete_group2))[2].click()

    def check_delete(self):
        time.sleep(1)
        d1 = self.driver.find_elements(By.XPATH, f"//*[text()='{self.name}{self.b}']")    #找不到返回[]
        assert len(d1)==0
        print("已断言，删除成功")



    def add_label(self,name1):
        self.name1 = name1
        time.sleep(1)
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located(add_label1))[1].click()     # 用户
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_element_located(add_label2)).click()    #用户标签
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_element_located(add_label3)).click()   #添加
        time.sleep(1)


        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located(add_label4))[2].click()    #展开下拉框
        time.sleep(1)
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located(add_label5))[random.randint(5,10)].click()    #随机选择

        time.sleep(2)
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located(add_label6))[3].send_keys(self.name1+str(self.b))     #输入
        time.sleep(2)
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located(add_label7))[2].click()    #确定

    def check_tabel_add(self):
        time.sleep(5)
        c1 = self.driver.find_element(By.XPATH, f"//*[text()='{self.name1}{self.b}']").text
        assert c1 == self.name1 + str(self.b)

    def delete_label(self):
        time.sleep(1)
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(delete_label1))[-1].click()
        time.sleep(1)
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(delete_label2))[2].click()

    def check_delete_label(self):
        time.sleep(1)
        f1 = self.driver.find_elements(By.XPATH, f"//*[text()='{self.name1}{self.b}']")
        assert len(f1) == 0
        print("已断言，label删除成功")


if __name__ == '__main__':
    driver = webdriver.Chrome()  # 初始化驱动
    aaa=CRMEB(driver,"http://120.55.83.187:8899/admin")
     #用户分组
    aaa.login("admin","A123456!")
    aaa.add_group("略略略")
    aaa.check_add()
    aaa.delete_group()
    aaa.check_delete()    #断言删除

    #标签
    aaa.add_label("哈哈哈")
    aaa.check_tabel_add()
    aaa.delete_label()
    aaa.check_delete_label()








# qq音乐账号密码形式登录，需要验证码：
# driver=webdriver.Chrome()   #初始化driver
# driver.get("https://y.qq.com/")
# driver.maximize_window()
# driver.implicitly_wait(10)
# driver.find_element(By.CLASS_NAME,"top_login__link").click()  #点击登录
# time.sleep(5)
# a=driver.find_elements(By.TAG_NAME,"iframe")[0]   #处理iframe框架
# driver.switch_to.frame(a)     #进入iframe 1框架
# b=driver.find_element(By.TAG_NAME,"iframe")
# driver.switch_to.frame(b)      # #进入iframe 2框架
# driver.find_element(By.ID,"switcher_plogin").click()              #切换成账号登录
# time.sleep(2)
# driver.find_elements(By.CLASS_NAME,"inputstyle")[0].send_keys("xxxxxx") #输入账号
# driver.find_elements(By.CLASS_NAME,"inputstyle")[1].send_keys("xxxxxxxxxx")   #输入密码
# driver.find_element(By.ID,"login_button").click()     #登录
# time.sleep(2)

# qq音乐登录，直接点击头像登录：
# driver=webdriver.Chrome()   #初始化driver
# driver.get("https://y.qq.com/")
# driver.maximize_window()
# driver.implicitly_wait(10)
# driver.find_element(By.CLASS_NAME,"top_login__link").click()  #点击登录
# time.sleep(5)
# a=driver.find_elements(By.TAG_NAME,"iframe")[0]   #处理iframe框架
# driver.switch_to.frame(a)     #进入iframe 1框架
# b=driver.find_element(By.TAG_NAME,"iframe")
# driver.switch_to.frame(b)      # #进入iframe 2框架
# driver.find_element(By.ID,"img_out_1252675579").click()   #点击头像直接登录
# time.sleep(2)

